/*---------------------------------------
*	input:			ium_data_clean.dta
				
*	output:			
				
*	author:			Liu Huidi
*	create_date: 	2020-01-03
*	last_modify:	2021-02-23				

---------------------------------------*/
	clear all
	set maxvar 32767

/*	set directories————Huidi  */	
	global dtadir ""
	global workingdata ""
	global results ""


********************************************************************************		

	use $dtadir/ium_data_clean.dta, clear
*	keep if spv330 == 1 //gen appendix table 1/4/5
	
********************************************************************************	
***********************************table 1**************************************	
*****************characteristics of doctors and clinics*************************	
********************************************************************************	
********************************************************************************	
	
/*	table 1. characteristics of doctors and clinics  */	
//Received any training on asthma in the previous two years (0)
//Instruments for pulmonary ventilation test (yes=1) (0)
//Instruments for bronchodilator test (yes=1) (0)
//Instruments for percussion added to chest examination (yes=1) (All)

	local binaries " doc_male doc_edu1 doc_edu2 doc_edu3 doc_cert doc_train_imu doc_pathway fac_zero stethoscope sphygmomanometer xray CBC fac_drip fac_inje fac_pres_record fac_pres_assess "
	
	foreach var of varlist ///
		doc_male doc_age ///
		doc_edu1 doc_edu2 doc_edu3 ///
		doc_cert ///
		doc_pathway ///
		doc_train_imu ///
		doc_incpct ///
		doc_num1 ///
		fac_patients ///
		fac_zero ///
		fac_inventory_1 ///
		fac_inventory_2 ///
		stethoscope ///
		sphygmomanometer ///
		xray ///
		CBC ///
		fac_drip ///
		fac_inje ///
		fac_pres_record ///
		fac_pres_assess {
			
				sum `var' 
					local f = `r(N)'
				if regexm("`binaries'"," `var' ") local a = `r(mean)'*100
					else local a = `r(mean)'
				local b = `r(sd)'
				local c = `r(min)'
				local d = `r(max)'
				if regexm("`binaries'"," `var' ") local e = `r(sum)'
					else local e = .

			
			mat table1 = nullmat(table1) \ [`f',`e',`a',`b',`c',`d']
		}
		
		local columns `" "N" "n" "Mean" "Standard Deviation" "Min" "Max" "'
		
		#delimit ;
		local rows `" 
		"Male (yes=1)"
		"Age (year)"
		"Junior high school or lower education (yes=1)"
		"Senior high school (yes=1)"
		"Junior college or higher education (yes=1)"
		"(Associate) Medical practitioner (yes=1)"
		"Familiarity with the terminology of clinical pathway (yes=1)"
		"Receiving any trainings on irrational use of antibiotics in the previous two years (yes=1)"
		"% of income from working in clinics (%)"
		"Number of village doctors (person)"
		"Number of patient visits in the previous month of the survey (encounters)"
		"Implementation of zero-mark-up drug policy (yes=1)"
		"Varieties of western medicines at stock in the clinic"
		"Varieties of Chinese patent medicines at stock in the clinic" 
		"Instruments for chest auscultation (yes=1)"
		"Instruments for assessment of heart rate, pulse, or blood pressure (yes=1)" 
		"Instruments for chest X-ray examination (yes=1)"
		"Instruments for blood routine test (yes=1)"
		"Availability of intravenous drip service (yes=1)"
		"Availability of intramuscular injection service (yes=1)"
		"Availability of medical records (yes=1)"
		"Any incentives designed by the upper government for their performance in prescription assessment (yes=1)"
		"' ;
		#delimit cr
		
		
		xml_tab ///
			table1 ///
			using "$results/table1.xls" ///
			, replace ///
			title("Characteristics of doctors and clinics") ///
			sheet("Table 1") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			


********************************************************************************	
*********************************Appendix 1*************************************	
*********************************Knowledge**************************************	
*************************run "keep if spv330 == 1" first************************	
********************************************************************************
/*
	gen pres = 0 if k_pres == 0 & spv330_drugpres == 0
		replace pres = 1 if k_pres == 1 & spv330_drugpres == 0
		replace pres = 2 if k_pres == 0 & spv330_drugpres == 1
		replace pres = 3 if k_pres == 1 & spv330_drugpres == 1

	local binaries " doc_male doc_edu1 doc_edu2 doc_edu3 doc_cert doc_train_imu doc_pathway fac_zero stethoscope sphygmomanometer xray CBC fac_drip fac_inje fac_pres_record fac_pres_assess "
	
	foreach var of varlist ///
		doc_male doc_age ///
		doc_edu1 doc_edu2 doc_edu3 ///
		doc_cert ///
		doc_pathway ///
		doc_train_imu ///
		doc_incpct ///
		doc_num1 ///
		fac_patients ///
		fac_zero ///
		fac_inventory_1 ///
		fac_inventory_2 ///
		stethoscope ///
		sphygmomanometer ///
		xray ///
		CBC ///
		fac_drip ///
		fac_inje ///
		fac_pres_record ///
		fac_pres_assess {

				sum `var'  if pres == 1
					local h = `r(N)'
				if regexm("`binaries'"," `var' ") local e = `r(mean)'*100
					else local e = `r(mean)'
				local f = `r(sd)'
				if regexm("`binaries'"," `var' ") local g = `r(sum)'
					else local g = .
					
				sum `var' if pres == 2
					local i = `r(N)'
				if regexm("`binaries'"," `var' ") local j = `r(mean)'*100
					else local j = `r(mean)'
				local k = `r(sd)'
				if regexm("`binaries'"," `var' ") local l = `r(sum)'
					else local l = .
					
					
			mat table1_pres1 = nullmat(table1_pres1) \ [`h',`g',`e',`f',`i',`l',`j',`k']
		}
		
		local columns `" "N" "n" "Mean" "Standard Deviation" "N" "n" "Mean" "Standard Deviation" "'
		
		#delimit ;
		local rows `" 
		"Male (yes=1)"
		"Age (year)"
		"Junior high school or lower education (yes=1)"
		"Senior high school (yes=1)"
		"Junior college or higher education (yes=1)"
		"(Associate) Medical practitioner (yes=1)"
		"Familiarity with the terminology of clinical pathway (yes=1)"
		"Receiving any trainings on irrational use of medicine in the previous two years (yes=1)"
		"% of income from working in clinics (%)"
		"Number of village doctors (person)"
		"Number of patient visits in the previous month of the survey (encounters)"
		"Implementation of zero-mark-up drug policy (yes=1)"
		"Varieties of western medicines at stock in the clinic"
		"Varieties of Chinese patent medicines at stock in the clinic" 
		"Instruments for chest auscultation (yes=1)"
		"Instruments for assessment of heart rate, pulse, or blood pressure (yes=1)" 
		"Instruments for chest X-ray examination (yes=1)"
		"Instruments for blood routine test (yes=1)"
		"Availability of intravenous drip service (yes=1)"
		"Availability of intramuscular injection service (yes=1)"
		"Availability of medical records (yes=1)"
		"Any incentives designed by the upper government for their performance in prescription assessment (yes=1)"
		"' ;
		#delimit cr
		
		
		xml_tab ///
			table1_pres1 ///
			using "$results/table1_pres1.xls" ///
			, replace ///
			title("Characteristics of doctors and clinics") ///
			sheet("Table 1_pres1") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))	
		

	local binaries " doc_male doc_edu1 doc_edu2 doc_edu3 doc_cert doc_train_imu doc_pathway fac_zero stethoscope sphygmomanometer xray CBC fac_drip fac_inje fac_pres_record fac_pres_assess "
	foreach var of varlist ///
		doc_male doc_age ///
		doc_edu1 doc_edu2 doc_edu3 ///
		doc_cert ///
		doc_pathway ///
		doc_train_imu ///
		doc_incpct ///
		doc_num1 ///
		fac_patients ///
		fac_zero ///
		fac_inventory_1 ///
		fac_inventory_2 ///
		stethoscope ///
		sphygmomanometer ///
		xray ///
		CBC ///
		fac_drip ///
		fac_inje ///
		fac_pres_record ///
		fac_pres_assess {


		ttest `var' if pres == 1 | pres == 2, by(pres)
			local a4 = `r(p)'
			local b4 = `r(se)'
			local c4 = `r(mu_2)'
			local d4 = `r(mu_1)'
			local e4 = `r(N_2)'
			local f4 = `r(N_1)'
		
		if regexm("`binaries'"," `var' ") {
			local diff4 = (`c4'-`d4')*100
			local lb4 = `diff4'-1.96*100*`b4'
			local ub4 = `diff4'+1.96*100*`b4'
			if `a4'<0.001 local p4 = .a
				else if (`a4'<0.01 & `a4'>=0.001) local p4 = .b
				else if (`a4'<0.05 & `a4'>=0.01) local p4 = .c
				else local p4 = .d
		}
		else {
			local diff4 = (`c4'-`d4')
			local lb4 = `diff4'-1.96*`b4'
			local ub4 = `diff4'+1.96*`b4'
			if `a4'<0.001 local p4 = .a
				else if (`a4'<0.01 & `a4'>=0.001) local p4 = .b
				else if (`a4'<0.05 & `a4'>=0.01) local p4 = .c
				else local p4 = .d
		}



		mat table1_pres2 = nullmat(table1_pres2) \ [`diff4',`lb4',`ub4',`p4',`e4',`f4']

	}	
	
		local columns `" "diff" "LB" "UB" "P" "N1" "N2" "'

		#delimit ;
		local rows `" 
		"Male (yes=1)"
		"Age (year)"
		"Junior high school or lower education (yes=1)"
		"Senior high school (yes=1)"
		"Junior college or higher education (yes=1)"
		"(Associate) Medical practitioner (yes=1)"
		"Familiarity with the terminology of clinical pathway (yes=1)"
		"Receiving any trainings on irrational use of medicine in the previous two years (yes=1)"
		"% of income from working in clinics (%)"
		"Number of village doctors (person)"
		"Number of patient visits in the previous month of the survey (encounters)"
		"Implementation of zero-mark-up drug policy (yes=1)"
		"Varieties of western medicines at stock in the clinic"
		"Varieties of Chinese patent medicines at stock in the clinic" 
		"Instruments for chest auscultation (yes=1)"
		"Instruments for assessment of heart rate, pulse, or blood pressure (yes=1)" 
		"Instruments for chest X-ray examination (yes=1)"
		"Instruments for blood routine test (yes=1)"
		"Availability of intravenous drip service (yes=1)"
		"Availability of intramuscular injection service (yes=1)"
		"Availability of medical records (yes=1)"
		"Any incentives designed by the upper government for their performance in prescription assessment (yes=1)"
		"' ;
		#delimit cr
		
		
		xml_tab ///
			table1_pres2 ///
			using "$results/table1_pres2.xls" ///
			, replace ///
			title("Characteristics of doctors and clinics") ///
			sheet("Table 1_pres2") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			
*/	
********************************************************************************	
********************************************************************************	
*********************************Knowledge**************************************	
********************************************************************************	
********************************************************************************	
		
/*	Table Ap2 or 4 - K1: A Presumptive Asthma-part1	*/
	
	local binaries " k_pres k_chionly k_westonly k_westchi "
	
	foreach var of varlist k_pres  {
	
		replace `var' = 0 if `var' == .
			
		if regexm("`binaries'"," `var' ") count if `var' == 1
			local a = `r(N)'
			else local a = .	
		sum `var'
			if regexm("`binaries'"," `var' ") local b = `r(mean)'*100
				else local b = `r(mean)'
			local c = `r(sd)'
			local d = `r(N)'
			
		mat table_k1 = nullmat(table_k1) \ [`a',`b',`c',`d']
	
	}

	foreach var of varlist k_chionly k_westonly k_westchi k_drug_num k_injection_num ///
				   k_other_num k_drug_num_chi {
	
		replace `var' = . if vig430_drug != 1
			
		if regexm("`binaries'"," `var' ") count if `var' == 1
			local a = `r(N)'
			else local a = .	
		sum `var'
			if regexm("`binaries'"," `var' ") local b = `r(mean)'*100
				else local b = `r(mean)'
			local c = `r(sd)'
			local d = `r(N)'
			
		mat table_k1 = nullmat(table_k1) \ [`a',`b',`c',`d']
	
	}


		local columns `" "n" "Mean" "SD" "N" "'
		
		#delimit ;
		local rows `" 
		"1. Encounters with prescriptions"
		"3. Encounters where only Chinese patent medicines were prescribed"
		"4. Encounters where only western medicines were prescribed"
		"5. Encounters where both Chinese patent medicines and western medicines were prescribed"
		"6. Number of medicines prescribed per encounter"
		"7. Number of medicines prescribed for injections per encounter"
		"8. Number of non-injection medicines per encounter"
		"9. Number of Chinese patent medicines prescribed per encounter"
		"' ;
		#delimit cr
	
		xml_tab ///
			table_k1 ///
			using "$results/table_k1.xls" ///
			, replace ///
			title("K1") ///
			sheet("Table K1") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			



/*	table Ap 3 or 5 - K2 IMU  */					 
	
	foreach var of varlist k_corrdrugonly k_unnedrugonly k_corrunne ///
						   k_imu_bi k_antibiotics_if k_injection_nochi k_drug_num5_nochi ///
						   k_incompatibility k_repe_dose k_adverse_ddi  {
		
		replace `var' = . if vig430_drug != 1
		
			count if `var' == 1 & k_west == 1
				local a = `r(N)'			
		
		sum `var' if k_west == 1
			local b = `r(mean)'*100
			local c = `r(sd)'
			local d = `r(N)'
		
		mat table_k2 = nullmat(table_k2) \ [`a',`b',`c',`d']
	}	
	
	
		local columns `" "n" "Mean" "SD" "N" "'
		
		#delimit ;
		local rows `" 
		"(1) Correct medicines prescribed only"
		"(2) Unnecessary medicines prescribed only"
		"(3) Both correct and unnecessary medicines prescribed"
		"Encounters with any of the following irrational use of medicines" 
		"(1) Antibiotics prescribed" 
		"(2) Injections prescribed"
		"(3) Polypharmacy (≥5 drugs)"
		"(4) Incompatible medicines prescribed" 
		"(5) Repetitive medicines prescribed" 
		"(6) Medicines prescribed having adverse interaction"	
		"' ;
		#delimit cr
		
		xml_tab ///
			table_k2 ///
			using "$results/table_k2.xls" ///
			, replace ///
			title("K2") ///
			sheet("Table K2") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			

	
********************************************************************************	
********************************************************************************	
**********************************Capacity**************************************	
********************************************************************************	
********************************************************************************		
/*	Table Ap2 or 4 - C1: A Presumptive Asthma-part2	*/

	local binaries " c_pres c_chionly c_westonly c_westchi "
	foreach var of varlist c_pres  {
	
		replace `var' = 0 if `var' == .
			
		if regexm("`binaries'"," `var' ") count if `var' == 1
			local a = `r(N)'
			else local a = .	
		sum `var'
			if regexm("`binaries'"," `var' ") local b = `r(mean)'*100
				else local b = `r(mean)'
			local c = `r(sd)'
			local d = `r(N)'
			
		mat table_c1 = nullmat(table_c1) \ [`a',`b',`c',`d']
	
	}
	foreach var of varlist c_chionly c_westonly c_westchi c_drug_num c_injection_num ///
				   c_other_num c_drug_num_chi {
	
		replace `var' = . if c_pres != 1
			
		if regexm("`binaries'"," `var' ") count if `var' == 1
			local a = `r(N)'
			else local a = .	
		sum `var'
			if regexm("`binaries'"," `var' ") local b = `r(mean)'*100
				else local b = `r(mean)'
			local c = `r(sd)'
			local d = `r(N)'
			
		mat table_c1 = nullmat(table_c1) \ [`a',`b',`c',`d']
		
	}

		local columns `" "n" "Mean" "SD" "N" "'
		
		#delimit ;
		local rows `" 
		"1. Encounters with prescriptions"
		"3. Encounters where only Chinese patent medicines were prescribed"
		"4. Encounters where only western medicines were prescribed"
		"5. Encounters where both Chinese patent medicines and western medicines were prescribed"
		"6. Number of medicines prescribed per encounter"
		"7. Number of medicines prescribed for injections per encounter"
		"8. Number of non-injection medicines per encounter"
		"9. Number of Chinese patent medicines prescribed per encounter"
		"' ;
		#delimit cr
	
		xml_tab ///
			table_c1 ///
			using "$results/table_c1.xls" ///
			, replace ///
			title("C1") ///
			sheet("Table C1") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			


	
/*	Table Ap3 or 5 - C2 IMU	*/
	foreach var of varlist c_corrdrugonly c_unnedrugonly c_corrunne ///
						   c_imu_bi c_antibiotics_if c_injection_nochi c_drug_num5_nochi ///
						   c_incompatibility ///
				 		   c_repe_dose c_adverse_ddi c_improper_dose  {
		
		replace `var' = . if c_west != 1
		
			count if `var' == 1 & c_west == 1
				local a = `r(N)'			
		
		sum `var' if c_west == 1
			local b = `r(mean)'*100
			local c = `r(sd)'
			local d = `r(N)'
		
		mat table_c2 = nullmat(table_c2) \ [`a',`b',`c',`d']
	}	
	
	
		local columns `" "n" "Mean" "SD" "N" "'
		
		#delimit ;
		local rows `" 
		"(1) Correct medicines prescribed only"
		"(2) Unnecessary medicines prescribed only"
		"(3) Both correct and unnecessary medicines prescribed"
		"Encounters with any of the following irrational use of medicines" 
		"(1) Antibiotics prescribed" 
		"(2) Injections prescribed"
		"(3) Polypharmacy (≥5 drugs)"
		"(4) Incompatible medicines prescribed" 
		"(5) Repetitive medicines prescribed" 
		"(6) Medicines prescribed having adverse interaction"	
		"(7) Improper dosage prescribed"
		"' ;
		#delimit cr
		
		xml_tab ///
			table_c2 ///
			using "$results/table_c2.xls" ///
			, replace ///
			title("C2") ///
			sheet("Table C2") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			

			

********************************************************************************	
********************************************************************************	
*********************************Performance************************************	
********************************************************************************	
********************************************************************************	
/*	Table Ap2 or 4 -D1: A Presumptive Asthma	*/	

	local binaries " d_pres d_unknownpres d_chionly d_westonly d_westchi "
	foreach var of varlist d_pres d_unknownpres  {
	
		replace `var' = 0 if `var' == . & spv330 == 1
			
		if regexm("`binaries'"," `var' ") count if `var' == 1
			local a = `r(N)'
			else local a = .	
		sum `var'
			if regexm("`binaries'"," `var' ") local b = `r(mean)'*100
				else local b = `r(mean)'
			local c = `r(sd)'
			local d = `r(N)'
			
		mat table_d1 = nullmat(table_d1) \ [`a',`b',`c',`d']
	
	}
	foreach var of varlist d_chionly d_westonly d_westchi d_drug_num d_other_num d_drug_num_chi {
	
		replace `var' = . if spv330_drugpres != 1 | spv330_unknown_pres == 1
			
		if regexm("`binaries'"," `var' ") count if `var' == 1
			local a = `r(N)'
			else local a = .	
		sum `var'
			if regexm("`binaries'"," `var' ") local b = `r(mean)'*100
				else local b = `r(mean)'
			local c = `r(sd)'
			local d = `r(N)'
			
		mat table_d1 = nullmat(table_d1) \ [`a',`b',`c',`d']
	
	}

		local columns `" "n" "Mean" "SD" "N" "'
		
		#delimit ;
		local rows `" 
		"1. Encounters with prescriptions"
		"2. Encounters where prescribed medicines were packed together by a paper"
		"3. Encounters where only Chinese patent medicines were prescribed"
		"4. Encounters where only western medicines were prescribed"
		"5. Encounters where both Chinese patent medicines and western medicines were prescribed"
		"6. Number of medicines prescribed per encounter"
		"8. Number of non-injection medicines per encounter"
		"9. Number of Chinese patent medicines prescribed per encounter"
		"' ;
		#delimit cr
	
		xml_tab ///
			table_d1 ///
			using "$results/table_d1.xls" ///
			, replace ///
			title("D1") ///
			sheet("Table D1") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			


/*	Table Ap3 or 5 - D2 IUM	*/
	replace d_injection_nochi = 0
	foreach var of varlist d_corrdrugonly d_unnedrugonly d_corrunne ///
						   d_imu_bi d_antibiotics_if d_injection_nochi d_drug_num5_nochi ///
						   d_incompatibility ///
				 		   d_repe_dose d_adverse_ddi d_improper_dose  {
		
		replace `var' = . if spv330_drugpres != 1 | spv330_unknown_pres == 1
		
			count if `var' == 1 & d_west == 1
				local a = `r(N)'			
		
		sum `var' if d_west == 1
			local b = `r(mean)'*100
			local c = `r(sd)'
			local d = `r(N)'
		
		mat table_d2 = nullmat(table_d2) \ [`a',`b',`c',`d']
	}	
	
	
		local columns `" "n" "Mean" "SD" "N" "'
		
		#delimit ;
		local rows `" 
		"(1) Correct medicines prescribed only"
		"(2) Unnecessary medicines prescribed only"
		"(3) Both correct and unnecessary medicines prescribed"
		"Encounters with any of the following irrational use of medicines" 
		"(1) Antibiotics prescribed" 
		"(2) Injections prescribed"
		"(3) Polypharmacy (≥5 drugs)"
		"(4) Incompatible medicines prescribed" 
		"(5) Repetitive medicines prescribed" 
		"(6) Medicines prescribed having adverse interaction"	
		"(7) Improper dosage prescribed"
		"' ;
		#delimit cr
		
		xml_tab ///
			table_d2 ///
			using "$results/table_d2.xls" ///
			, replace ///
			title("D2") ///
			sheet("Table D2") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			

		
********************************************************************************	
********************************************************************************	
********************************Know gap************************************	
********************************************************************************	
********************************************************************************		

/*	Ap3 or 5 - K Gap 2 */
	

	gen k_corrdrugonly1 = 1 - k_corrdrugonly if k_west == 1
	gen zero = 0 if k_west == 1
	foreach x in corrdrugonly1 unnedrugonly corrunne ///
				 imu_bi antibiotics_if injection_nochi drug_num5_nochi ///
				 incompatibility repe_dose adverse_ddi  {
	
		replace k_`x' = . if k_west != 1

		ttest k_`x' = zero
			local a = `r(p)'
			local b = `r(se)'
			local c = `r(mu_1)'
			local d = `r(mu_2)'
			local e = `r(N_1)'
			local f = `r(N_2)'
		
	
		local diff = (`c'-`d')*100
		local lb = `diff'-1.96*100*`b'
		local ub = `diff'+1.96*100*`b'
			if `a'<0.001 local p = .a
				else if (`a'<0.01 & `a'>=0.001) local p = .b
				else if (`a'<0.05 & `a'>=0.01) local p = .c
				else local p = .d
		
		
		mat table_kgap2 = nullmat(table_kgap2) \ [`diff',`p',`a',`lb',`ub',`e']

	}	


		local columns `" "diff" "P" "p value" "lb" "ub" "N" "'

		#delimit ;
		local rows `" 
		"(1) Correct medicines prescribed only"
		"(2) Unnecessary medicines prescribed only"
		"(3) Both correct and unnecessary medicines prescribed"
		"Encounters with any of the following irrational use of medicines" 
		"(1) Antibiotics prescribed" 
		"(2) Injections prescribed"
		"(3) Polypharmacy (≥5 drugs)"
		"(4) Incompatible medicines prescribed" 
		"(5) Repetitive medicines prescribed" 
		"(6) Medicines prescribed having adverse interaction"	
		"' ;
		#delimit cr
		
		xml_tab ///
			table_kgap2 ///
			using "$results/table_kgap2.xls" ///
			, replace ///
			title("K gap 2") ///
			sheet("Table Kgap2") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			
						
	
********************************************************************************	
********************************************************************************	
********************************Know-can gap************************************	
********************************************************************************	
********************************************************************************	
/*	Ap2 or 4 - KC 1 */

	local binaries " pres chionly westonly westchi westinjeonly chiinjeonly chiwestinje "
	foreach x in pres chionly westonly westchi ///
				   drug_num injection_num ///
				    other_num drug_num_chi {
				 
		ttest k_`x' = c_`x', unpaired
			local a = `r(p)'
			local b = `r(se)'
			local c = `r(mu_1)'
			local d = `r(mu_2)'
			local e = `r(N_1)'
			local f = `r(N_2)'
		
		if regexm("`binaries'"," `x' "){
			local diff = (`c'-`d')*100
			local lb = `diff'-1.96*100*`b'
			local ub = `diff'+1.96*100*`b'
			if `a'<0.001 local p = .a
				else if (`a'<0.01 & `a'>=0.001) local p = .b
				else if (`a'<0.05 & `a'>=0.01) local p = .c
				else local p = .d
		}
		else {
			local diff = (`c'-`d')
			local lb = `diff'-1.96*`b'
			local ub = `diff'+1.96*`b'
			if `a'<0.001 local p = .a
				else if (`a'<0.01 & `a'>=0.001) local p = .b
				else if (`a'<0.05 & `a'>=0.01) local p = .c
				else local p = .d
		}
		
		mat table_kc1 = nullmat(table_kc1) \ [`diff',`lb',`ub',`p',`a',`e',`f']

	}	


		local columns `" "diff" "LB" "UB" "P" "p value" "N1" "N2" "'
		
		#delimit ;
		local rows `" 
		"1. Encounters with prescriptions"
		"3. Encounters where only Chinese patent medicines were prescribed"
		"4. Encounters where only western medicines were prescribed"
		"5. Encounters where both Chinese patent medicines and western medicines were prescribed"
		"6. Number of medicines prescribed per encounter"
		"7. Number of medicines prescribed for injections per encounter"
		"8. Number of non-injection medicines per encounter"
		"9. Number of Chinese patent medicines prescribed per encounter"
		"' ;
		#delimit cr
		xml_tab ///
			table_kc1 ///
			using "$results/table_kc1.xls" ///
			, replace ///
			title("KC1") ///
			sheet("Table KC1") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			

/*	Ap3 or 5 - KC 2 */
	foreach x in corrdrugonly unnedrugonly corrunne ///
				 imu_bi antibiotics_if injection_nochi drug_num5_nochi ///
				 incompatibility repe_dose  adverse_ddi  {
	
		replace k_`x' = . if k_west != 1
		replace c_`x' = . if c_west != 1

		ttest k_`x' = c_`x', unpaired
			local a = `r(p)'
			local b = `r(se)'
			local c = `r(mu_1)'
			local d = `r(mu_2)'
			local e = `r(N_1)'
			local f = `r(N_2)'
		
			local diff = (`c'-`d')*100
			local lb = `diff'-1.96*100*`b'
			local ub = `diff'+1.96*100*`b'
			if `a'<0.001 local p = .a
				else if (`a'<0.01 & `a'>=0.001) local p = .b
				else if (`a'<0.05 & `a'>=0.01) local p = .c
				else local p = .d
		
		mat table_kc2 = nullmat(table_kc2) \ [`diff',`lb',`ub',`p',`a',`e',`f']

	}	
		
		local columns `" "diff" "LB" "UB" "P" "p value" "N1" "N2" "'

		#delimit ;
		local rows `" 
		"(1) Correct medicines prescribed only"
		"(2) Unnecessary medicines prescribed only"
		"(3) Both correct and unnecessary medicines prescribed"
		"Encounters with any of the following irrational use of medicines" 
		"(1) Antibiotics prescribed" 
		"(2) Injections prescribed"
		"(3) Polypharmacy (≥5 drugs)"
		"(4) Incompatible medicines prescribed" 
		"(5) Repetitive medicines prescribed" 
		"(6) Medicines prescribed having adverse interaction"	
		"' ;
		#delimit cr
		
		xml_tab ///
			table_kc2 ///
			using "$results/table_kc2.xls" ///
			, replace ///
			title("KC2") ///
			sheet("Table KC2") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			
			
	
	
********************************************************************************	
********************************************************************************	
********************************can-do gap************************************	
********************************************************************************	
********************************************************************************	

/*	Ap2 or 4 -CD 1 */

	local binaries " pres chionly westonly westchi westinjeonly chiinjeonly chiwestinje "
	foreach x in pres chionly westonly westchi ///
				   drug_num other_num drug_num_chi {
				 
		ttest c_`x' = d_`x', unpaired
			local a = `r(p)'
			local b = `r(se)'
			local c = `r(mu_1)'
			local d = `r(mu_2)'
			local e = `r(N_1)'
			local f = `r(N_2)'
		
		if regexm("`binaries'"," `x' "){
			local diff = (`c'-`d')*100
			local lb = `diff'-1.96*100*`b'
			local ub = `diff'+1.96*100*`b'
			if `a'<0.001 local p = .a
				else if (`a'<0.01 & `a'>=0.001) local p = .b
				else if (`a'<0.05 & `a'>=0.01) local p = .c
				else local p = .d
		}
		else {
			local diff = (`c'-`d')
			local lb = `diff'-1.96*`b'
			local ub = `diff'+1.96*`b'
			if `a'<0.001 local p = .a
				else if (`a'<0.01 & `a'>=0.001) local p = .b
				else if (`a'<0.05 & `a'>=0.01) local p = .c
				else local p = .d
		}
		
		mat table_cd1 = nullmat(table_cd1) \ [`diff',`lb',`ub',`p',`a',`e',`f']

	}	
	

		local columns `" "diff" "LB" "UB" "P" "p value" "N1" "N2" "'
		
		#delimit ;
		local rows `" 
		"1. Encounters with prescriptions"
		"3. Encounters where only Chinese patent medicines were prescribed"
		"4. Encounters where only western medicines were prescribed"
		"5. Encounters where both Chinese patent medicines and western medicines were prescribed"
		"6. Number of medicines prescribed per encounter"
		"8. Number of non-injection medicines per encounter"
		"9. Number of Chinese patent medicines prescribed per encounter"
		"' ;
		#delimit cr
		xml_tab ///
			table_cd1 ///
			using "$results/table_cd1.xls" ///
			, replace ///
			title("CD1") ///
			sheet("Table CD1") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			

/*	Ap3 or 5 - CD 2 */
	foreach x in corrdrugonly unnedrugonly corrunne ///
				 imu_bi antibiotics_if injection_nochi drug_num5_nochi ///
				 incompatibility ///
				 repe_dose  adverse_ddi improper_dose {
	
		replace d_`x' = . if d_west != 1

		ttest c_`x' = d_`x', unpaired
			local a = `r(p)'
			local b = `r(se)'
			local c = `r(mu_1)'
			local d = `r(mu_2)'
			local e = `r(N_1)'
			local f = `r(N_2)'
		
			local diff = (`c'-`d')*100
			local lb = `diff'-1.96*100*`b'
			local ub = `diff'+1.96*100*`b'
			if `a'<0.001 local p = .a
				else if (`a'<0.01 & `a'>=0.001) local p = .b
				else if (`a'<0.05 & `a'>=0.01) local p = .c
				else local p = .d
		
		mat table_cd2 = nullmat(table_cd2) \ [`diff',`lb',`ub',`p',`a',`e',`f']

	}	
		
		local columns `" "diff" "LB" "UB" "P" "p value" "N1" "N2" "'

		#delimit ;
		local rows `" 
		"(1) Correct medicines prescribed only"
		"(2) Unnecessary medicines prescribed only"
		"(3) Both correct and unnecessary medicines prescribed"
		"Encounters with any of the following irrational use of medicines" 
		"(1) Antibiotics prescribed" 
		"(2) Injections prescribed"
		"(3) Polypharmacy (≥5 drugs)"
		"(4) Incompatible medicines prescribed" 
		"(5) Repetitive medicines prescribed" 
		"(6) Medicines prescribed having adverse interaction"	
		"(7) Improper dosage prescribed"
		"' ;
		#delimit cr
		
		xml_tab ///
			table_cd2 ///
			using "$results/table_cd2.xls" ///
			, replace ///
			title("CD2") ///
			sheet("Table CD2") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			
			

********************************************************************************	
********************************************************************************	
**********************************total gap*************************************	
********************************************************************************	
********************************************************************************	

/*	Ap3 or 5 -Total Gap 2 */

	gen d_corrdrugonly1 = 1 - d_corrdrugonly if d_west == 1
	gen d_zero = 0 if d_west == 1
	foreach x in corrdrugonly1 unnedrugonly corrunne ///
				 imu_bi antibiotics_if injection_nochi drug_num5_nochi ///
				 incompatibility repe_dose adverse_ddi improper_dose  {
	
		replace d_`x' = . if d_west != 1

		ttest d_`x' = d_zero
			local a = `r(p)'
			local b = `r(se)'
			local c = `r(mu_1)'
			local d = `r(mu_2)'
			local e = `r(N_1)'
			local f = `r(N_2)'
		
	
		local diff = (`c'-`d')*100
		local lb = `diff'-1.96*100*`b'
		local ub = `diff'+1.96*100*`b'
			if `a'<0.001 local p = .a
				else if (`a'<0.01 & `a'>=0.001) local p = .b
				else if (`a'<0.05 & `a'>=0.01) local p = .c
				else local p = .d
		
		
		mat table_dgap2 = nullmat(table_dgap2) \ [`diff',`p',`a',`lb',`ub',`e']

	}	


		local columns `" "diff" "P" "p value" "lb" "ub" "N" "'

		#delimit ;
		local rows `" 
		"(1) Correct medicines prescribed only"
		"(2) Unnecessary medicines prescribed only"
		"(3) Both correct and unnecessary medicines prescribed"
		"Encounters with any of the following irrational use of medicines" 
		"(1) Antibiotics prescribed" 
		"(2) Injections prescribed"
		"(3) Polypharmacy (≥5 drugs)"
		"(4) Incompatible medicines prescribed" 
		"(5) Repetitive medicines prescribed" 
		"(6) Medicines prescribed having adverse interaction"	
		"(7) Improper dosage prescribed"
		"' ;
		#delimit cr
		
		xml_tab ///
			table_dgap2 ///
			using "$results/table_dgap2.xls" ///
			, replace ///
			title("Total gap 2") ///
			sheet("Table dgap2") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))			
						

	
********************************************************************************	
********************************Appendix 6**************************************	
****************correlation of antibiotics use and doc chars********************	
********************************************************************************	
********************************************************************************	
	clonevar k_antibiotics_if1 = k_antibiotics_if 
	replace k_antibiotics_if1 = 0 if k_antibiotics_if ==  . & k_pres == 1
	clonevar c_antibiotics_if1 = c_antibiotics_if 
	replace c_antibiotics_if1 = 0 if c_antibiotics_if ==  . & c_pres == 1
	clonevar d_antibiotics_if1 = d_antibiotics_if 
	replace d_antibiotics_if1 = 0 if d_antibiotics_if ==  . & d_pres == 1 & d_unknownpres != 1
	
//Received any training on asthma in the previous two years (0)
//Instruments for pulmonary ventilation test (yes=1) (0)
//Instruments for bronchodilator test (yes=1) (0)
//Instruments for percussion added to chest examination (yes=1) (All)

	foreach var of varlist ///
		doc_male doc_age ///
		doc_edu1 doc_edu2 doc_edu3 ///
		doc_cert ///
		doc_pathway ///
		doc_train_imu ///
		doc_incpct ///
		doc_num1 ///
		fac_patients ///
		fac_zero ///
		fac_inventory_1 ///
		fac_inventory_2 ///
		stethoscope ///
		sphygmomanometer ///
		xray ///
		CBC ///
		fac_drip ///
		fac_inje ///
		fac_pres_record ///
		fac_pres_assess {

			logit k_antibiotics_if1 `var', robust or
				mat kresults = r(table)
				local kor = kresults[1,1]
				local kpvalue = kresults[4,1]
				local klb = kresults[5,1]
				local kub = kresults[6,1]
			if `kpvalue'<0.001 local kp = .a
				else if (`kpvalue'<0.01 & `kpvalue'>=0.001) local kp = .b
				else if (`kpvalue'<0.05 & `kpvalue'>=0.01) local kp = .c
				else local kp = .d

			logit c_antibiotics_if1 `var', robust or
				mat cresults = r(table)
				local cor = cresults[1,1]
				local cpvalue = cresults[4,1]
				local clb = cresults[5,1]
				local cub = cresults[6,1]
			if `cpvalue'<0.001 local cp = .a
				else if (`cpvalue'<0.01 & `cpvalue'>=0.001) local cp = .b
				else if (`cpvalue'<0.05 & `cpvalue'>=0.01) local cp = .c
				else local cp = .d

			logit d_antibiotics_if1 `var', robust or
				mat dresults = r(table)
				local dor = dresults[1,1]
				local dpvalue = dresults[4,1]
				local dlb = dresults[5,1]
				local dub = dresults[6,1]
			if `dpvalue'<0.001 local dp = .a
				else if (`dpvalue'<0.01 & `dpvalue'>=0.001) local dp = .b
				else if (`dpvalue'<0.05 & `dpvalue'>=0.01) local dp = .c
				else local dp = .d	

			mat table_correlation_antibiotics = nullmat(table_correlation_antibiotics) \ [`kor',`kpvalue',`kp',`klb',`kub',`cor',`cpvalue',`cp',`clb',`cub',`dor',`dpvalue',`dp',`dlb',`dub']			

		}	

		local columns `" "kor" "kpvalue" "kp" "klb" "kub" "cor" "cpvalue" "cp" "clb" "cub" "dor" "dpvalue" "dp" "dlb" "dub" "'
		
		#delimit ;
		local rows `" 
		"Male (yes=1)"
		"Age (year)"
		"Junior high school or lower education (yes=1)"
		"Senior high school (yes=1)"
		"Junior college or higher education (yes=1)"
		"(Associate) Medical practitioner (yes=1)"
		"Familiarity with the terminology of clinical pathway (yes=1)"
		"Receiving any trainings on irrational use of antibiotics in the previous two years (yes=1)"
		"% of income from working in clinics (%)"
		"Number of village doctors (person)"
		"Number of patient visits in the previous month of the survey (encounters)"
		"Implementation of zero-mark-up drug policy (yes=1)"
		"Varieties of western medicines at stock in the clinic"
		"Varieties of Chinese patent medicines at stock in the clinic" 
		"Instruments for chest auscultation (yes=1)"
		"Instruments for assessment of heart rate, pulse, or blood pressure (yes=1)" 
		"Instruments for chest X-ray examination (yes=1)"
		"Instruments for blood routine test (yes=1)"
		"Availability of intravenous drip service (yes=1)"
		"Availability of intramuscular injection service (yes=1)"
		"Availability of medical records (yes=1)"
		"Any incentives designed by the upper government for their performance in prescription assessment (yes=1)"
		"' ;
		#delimit cr
		
		
		xml_tab ///
			table_correlation_antibiotics ///
			using "$results/table_correlation_antibiotics.xls" ///
			, replace ///
			title("correlation_antibiotics") ///
			sheet("table_correlation_antibiotics") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))		




********************************************************************************	
********************************Appendix 7**************************************	
****************************K: by (p)correct diag*******************************	
********************************************************************************	
********************************************************************************	
/*	K by Diagnosis 1 */

	local binaries " pres chionly westonly westchi "
	foreach x in pres chionly westonly westchi ///
				   drug_num injection_num ///
				   other_num drug_num_chi {
				 
		ttest k_`x' , by(k_correctdiag)
			local a = `r(p)'
			local b = `r(se)'
			local c = `r(mu_1)'
			local d = `r(mu_2)'
			local e = `r(N_1)'
			local f = `r(N_2)'
			
		sum k_`x' if k_correctdiag == 1
				local sd1 = `r(sd)'
		sum k_`x' if k_correctdiag == 0
				local sd0 = `r(sd)'
			

		if regexm("`binaries'"," `x' "){
			count if k_`x' == 1 & k_correctdiag == 1
				local n1 = `r(N)'
			count if k_`x' == 1 & k_correctdiag == 0
				local n0 = `r(N)'

			local mean1 = `d' * 100
			local mean0 = `c' * 100

			local diff = (`d'-`c')*100
			local lb = `diff'-1.96*100*`b'
			local ub = `diff'+1.96*100*`b'
			if `a'<0.001 local p = .a
				else if (`a'<0.01 & `a'>=0.001) local p = .b
				else if (`a'<0.05 & `a'>=0.01) local p = .c
				else local p = .d
		}

		else {
			local n1 = . 
			local n0 = .

			local mean1 = `d'
			local mean0 = `c'

			local diff = (`d'-`c')
			local lb = `diff'-1.96*`b'
			local ub = `diff'+1.96*`b'
			if `a'<0.001 local p = .a
				else if (`a'<0.01 & `a'>=0.001) local p = .b
				else if (`a'<0.05 & `a'>=0.01) local p = .c
				else local p = .d
		}
		
		mat table_kbycdiag1 = nullmat(table_kbycdiag1) \ [`mean1',`mean0',`n1',`n0',`sd1',`sd0',`diff',`lb',`ub',`p',`a',`f',`e']

	}	
		
		local columns `" "mean1" "mean0" "n1" "n0" "sd1" "sd0" "diff" "LB" "UB" "P" "p value" "N1" "N0" "'
		
		#delimit ;
		local rows `" 
		"1. Encounters with prescriptions"
		"3. Encounters where only Chinese patent medicines were prescribed"
		"4. Encounters where only western medicines were prescribed"
		"5. Encounters where both Chinese patent medicines and western medicines were prescribed"
		"6. Number of medicines prescribed per encounter"
		"7. Number of medicines prescribed for injections per encounter"
		"8. Number of non-injection medicines per encounter"
		"9. Number of Chinese patent medicines prescribed per encounter"
		"' ;
		#delimit cr
		xml_tab ///
			table_kbycdiag1 ///
			using "$results/table_kbycdiag1.xls" ///
			, replace ///
			title("K by diagnosis 1") ///
			sheet("Table k by correct diag1") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))


/*	K by Diagnosis 2 */
	foreach x in corrdrugonly unnedrugonly corrunne ///
				 imu_bi antibiotics_if injection_nochi drug_num5_nochi ///
				 incompatibility repe_dose  adverse_ddi  {
	
		replace k_`x' = . if k_west != 1

		ttest k_`x' , by(k_correctdiag)
			local a = `r(p)'
			local b = `r(se)'
			local c = `r(mu_1)'
			local d = `r(mu_2)'
			local e = `r(N_1)'
			local f = `r(N_2)'
			
			count if k_`x' == 1 & k_correctdiag == 1
				local n1 = `r(N)'
			count if k_`x' == 1 & k_correctdiag == 0
				local n0 = `r(N)'

			local mean1 = `d' * 100
			local mean0 = `c' * 100
		
			local diff = (`d'-`c')*100
			local lb = `diff'-1.96*100*`b'
			local ub = `diff'+1.96*100*`b'
			if `a'<0.001 local p = .a
				else if (`a'<0.01 & `a'>=0.001) local p = .b
				else if (`a'<0.05 & `a'>=0.01) local p = .c
				else local p = .d
		
		mat table_kbycdiag2 = nullmat(table_kbycdiag2) \ [`mean1',`mean0',`n1', `n0',`diff',`lb',`ub',`p',`a',`f',`e']

	}	
		
		local columns `" "mean1" "mean0" "n1" "n0" "diff" "LB" "UB" "P" "p value" "N1" "N0" "'

		#delimit ;
		local rows `" 
		"(1) Correct medicines prescribed only"
		"(2) Unnecessary medicines prescribed only"
		"(3) Both correct and unnecessary medicines prescribed"
		"Encounters with any of the following irrational use of medicines" 
		"(1) Antibiotics prescribed" 
		"(2) Injections prescribed"
		"(3) Polypharmacy (≥5 drugs)"
		"(4) Incompatible medicines prescribed" 
		"(5) Repetitive medicines prescribed" 
		"(6) Medicines prescribed having adverse interaction"	
		"' ;
		#delimit cr
		
		xml_tab ///
			table_kbycdiag2 ///
			using "$results/table_kbycdiag2.xls" ///
			, replace ///
			title("K by diagnosis 2") ///
			sheet("Table K by correct diag 2") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2))	

	
********************************************************************************	
********************************Appendix 8**************************************	
******************request for exam and capacity of devices**********************	
********************************************************************************	
********************************************************************************	

		ttest vig430_r_e1 = stethoscope //胸前背后听诊需要听诊器
		ttest vig430_r_e2 = 0 //需要肺量计，均无
		ttest vig430_r_e3 = 0 //需要肺量计，均无
		ttest vig430_r_e4 = sphygmomanometer //血压计
		ttest vig430_r_e5 = xray //胸部X光
		ttest vig430_r_e6 = CBC //血常规
		ttest vig430_r_e7 = 1 //叩诊，可以无需设备

		foreach num of numlist 1/7 {
			count if vig430_r_e`num' == 1
		}



********************************************************************************	
********************************Appendix 9**************************************	
***********IMU in prescriptions with both Chi and Western Medicines*************	
********************************************************************************	
********************************************************************************	

	//know
	foreach x in imu_bi antibiotics_if injection drug_num5 ///
				 incompatibility repe_dose  adverse_ddi  {
	
		replace k_`x'_nochi = . if k_pres != 1
		replace k_`x'_withchi = . if k_pres != 1
		replace k_`x'_nochi = 0 if k_pres == 1 & k_`x'_nochi == .
		replace k_`x'_withchi = 0 if k_pres == 1 & k_`x'_withchi == .
		
		
		count if k_`x'_nochi == 1
			local a=`r(N)'
		sum k_`x'_nochi
			local b = `r(mean)'*100
			local c = `r(N)'
			
		count if k_`x'_withchi == 1
			local d=`r(N)'
		sum k_`x'_withchi
			local e = `r(mean)'*100
			local f = `r(N)'

		ttest k_`x'_nochi = k_`x'_withchi, unpaired
			local g = `r(p)'
			local h = `r(se)'
			local i = `r(mu_1)'
			local j = `r(mu_2)'
			local k = `r(N_1)'
			local l = `r(N_2)'
		
			local diff = (`i'-`j')*100
			local lb = `diff'-1.96*100*`h'
			local ub = `diff'+1.96*100*`h'
			if `g'<0.001 local p = .a
				else if (`g'<0.01 & `g'>=0.001) local p = .b
				else if (`g'<0.05 & `g'>=0.01) local p = .c
				else local p = .d
		
	
			
		mat table_k_chi_nochi = nullmat(table_k_chi_nochi) \ [`a',`b',`c',`d',`e',`f',`diff',`lb',`ub',`p',`k',`l']
	}
	
		local columns `" "n" "Mean" "N" "n" "Mean" "N" "diff" "LB" "UB" "P" "N1" "N2" "'
		
		#delimit ;
		local rows `" 
		"Encounters with any of the following irrational use of medicines" 
		"(1) Antibiotics"
		"(2) Injections prescribed"
		"(3) Polypharmacy (≥5 drugs)"
		"(4) Incompatible medicines prescribed" 
		"(5) Repetitive medicines prescribed" 
		"(6) Medicines prescribed having adverse interaction"	
		"' ;
		#delimit cr

		xml_tab ///
			table_k_chi_nochi ///
			using "$results/table_k_chi_nochi.xls" ///
			, replace ///
			title("K IMU with and without consideration of Chi ") ///
			sheet("table_k_chi_nochi") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 2 LAST_ROW 2)  format((SCLB0) (SCCB0 NCRR2))			


	//can
	foreach x in imu_bi antibiotics_if injection drug_num5  ///
				 incompatibility ///
				 repe_dose  adverse_ddi improper_dose  {
	
		
		replace c_`x'_nochi = . if c_pres != 1
		replace c_`x'_withchi = . if c_pres != 1
		replace c_`x'_nochi = 0 if c_pres == 1 & c_`x'_nochi == .
		replace c_`x'_withchi = 0 if c_pres == 1 & c_`x'_withchi == .
		
		count if c_`x'_nochi == 1
			local a=`r(N)'
		sum c_`x'_nochi
			local b = `r(mean)'*100
			local c = `r(N)'
			
		count if c_`x'_withchi == 1
			local d=`r(N)'
		sum c_`x'_withchi
			local e = `r(mean)'*100
			local f = `r(N)'

		ttest c_`x'_nochi = c_`x'_withchi, unpaired
			local g = `r(p)'
			local h = `r(se)'
			local i = `r(mu_1)'
			local j = `r(mu_2)'
			local k = `r(N_1)'
			local l = `r(N_2)'
		
			local diff = (`i'-`j')*100
			local lb = `diff'-1.96*100*`h'
			local ub = `diff'+1.96*100*`h'
			if `g'<0.001 local p = .a
				else if (`g'<0.01 & `g'>=0.001) local p = .b
				else if (`g'<0.05 & `g'>=0.01) local p = .c
				else local p = .d
				
		mat table_c_chi_nochi = nullmat(table_c_chi_nochi) \ [`a',`b',`c',`d',`e',`f',`diff',`lb',`ub',`p',`k',`l']
	}
	
		local columns `" "n" "Mean" "N" "n" "Mean" "N" "diff" "LB" "UB" "P" "N1" "N2" "'
		
		#delimit ;
		local rows `" 		
		"Encounters with any of the following irrational use of medicines" 
		"(1) Antibiotics"
		"(2) Injections prescribed"
		"(3) Polypharmacy (≥5 drugs)"
		"(4) Incompatible medicines prescribed" 
		"(5) Repetitive medicines prescribed" 
		"(6) Medicines prescribed having adverse interaction"	
		"(7) Improper dosage prescribed"
		"' ;
		#delimit cr

		xml_tab ///
			table_c_chi_nochi ///
			using "$results/table_c_chi_nochi.xls" ///
			, replace ///
			title("C IMU with and without consideration of Chi ") ///
			sheet("table_c_chi_nochi") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 2 LAST_ROW 2)  format((SCLB0) (SCCB0 NCRR2))			
	
	
	//do
	foreach x in imu_bi antibiotics_if injection drug_num5  ///
				 incompatibility ///
				 repe_dose adverse_ddi improper_dose  {
	
		replace d_`x'_nochi = . if d_pres != 1 | spv330_unknown_pres == 1
		replace d_`x'_withchi = . if d_pres != 1 | spv330_unknown_pres == 1
		replace d_`x'_nochi = 0 if d_pres == 1 & spv330_unknown_pres != 1 & d_`x'_nochi == .
		replace d_`x'_withchi = 0 if d_pres == 1 & spv330_unknown_pres != 1 & d_`x'_withchi == .

		count if d_`x'_nochi == 1
			local a=`r(N)'
		sum d_`x'_nochi
			local b = `r(mean)'*100
			local c = `r(N)'
			
		count if d_`x'_withchi == 1
			local d=`r(N)'
		sum d_`x'_withchi
			local e = `r(mean)'*100
			local f = `r(N)'
			
		ttest d_`x'_nochi = d_`x'_withchi, unpaired
			local g = `r(p)'
			local h = `r(se)'
			local i = `r(mu_1)'
			local j = `r(mu_2)'
			local k = `r(N_1)'
			local l = `r(N_2)'
		
			local diff = (`i'-`j')*100
			local lb = `diff'-1.96*100*`h'
			local ub = `diff'+1.96*100*`h'
			if `g'<0.001 local p = .a
				else if (`g'<0.01 & `g'>=0.001) local p = .b
				else if (`g'<0.05 & `g'>=0.01) local p = .c
				else local p = .d
	
			mat table_d_chi_nochi = nullmat(table_d_chi_nochi) \ [`a',`b',`c',`d',`e',`f',`diff',`lb',`ub',`p',`k',`l']
	}
	
		local columns `" "n" "Mean" "N" "n" "Mean" "N" "diff" "LB" "UB" "P" "N1" "N2" "'
		
		#delimit ;
		local rows `" 		
		"Encounters with any of the following irrational use of medicines" 
		"(1) Antibiotics"
		"(2) Injections prescribed"
		"(3) Polypharmacy (≥5 drugs)"
		"(4) Incompatible medicines prescribed" 
		"(5) Repetitive medicines prescribed" 
		"(6) Medicines prescribed having adverse interaction"	
		"(7) Improper dosage prescribed"
		"' ;
		#delimit cr

		xml_tab ///
			table_d_chi_nochi ///
			using "$results/table_d_chi_nochi.xls" ///
			, replace ///
			title("D IMU with and without consideration of Chi ") ///
			sheet("table_d_chi_nochi") ///
			rnames(`rows') ///
			cnames(`columns') ///
			lines(COL_NAMES 2 LAST_ROW 2)  format((SCLB0) (SCCB0 NCRR2))			
	
,

********************************************************************************	
*********************************************************************************	
*********************************Flow Chart************************************	
********************************************************************************	
********************************************************************************	

//left
	tab k_pres,m
	
	tab k_chionly if k_pres == 1,m
	tab k_west if k_pres == 1,m
	
	tab c_pres if k_west != 1 & k_chi == 1
	
	tab k_westonly if k_west == 1,m
	tab k_westchi if k_west == 1,m
	
	tab c_pres if k_west == 1 & k_chi == 1
	tab c_pres if k_west == 1 & k_chi != 1

	
//right	
	tab d_pres if spv330 == 1,m
	tab d_unknownpres if spv330 == 1,m
	
	tab d_chionly if d_pres == 1 & d_unknownpres != 1,m
	tab d_west if d_pres == 1 & d_unknownpres != 1,m

	tab d_westonly if d_west == 1,m
	tab d_westchi if d_west == 1,m
		
